﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using MaxStoreErp.Core.Extensions;
using MaxStoreErp.Core.Interfaces;
using MaxStoreErp.Core.Threading;
using MaxStoreShop.Core.ViewerBase;
using MaxStoreShop.Domain.Directories;
using MaxStoreShop.Logic.ServicesContracts.Directories;
using Microsoft.Practices.Unity;

namespace MaxStoreShop.WarehouseModule.ProductCategories
{
    class ProductCategoriesPresenter : ViewerPresenterBase<ProductCategoriesView, ProductCategoriesViewModel, ProductCategory>, IToolBarProvider
    {
        [Dependency]
        public IProductCategoryService ProductCategoryService { get; set; }

        protected override void OnViewSet()
        {
            base.OnViewSet();
            ViewModel.TargetEditionElement = View.MainListView;
            ThreadHelper.SafeCall(() =>
            {
                ToolBarItems =
                    new ReadOnlyCollection<FrameworkElement>(View.MainToolBar.Items.Cast<FrameworkElement>().ToList());
            });
        }

        protected override void OnDeleteItem(ProductCategory removingItemType, CancelEventArgs cancelEventArgs)
        {
            base.OnDeleteItem(removingItemType, cancelEventArgs);
            ProductCategoryService.DeleteProductCategory(removingItemType);
        }

        protected override void ShowEditor(ProductCategory item)
        {
            base.ShowEditor(item);
            var editor = Container.Resolve<ProductCategoryEditor.ProductCategoryEditorPresenter>();
            editor.ViewModel.SelectedItem = item;
            Container.Resolve<IDocumentManager>().NavigateTo(editor.GetDocumentView());
        }

        protected override void OnDataLoading()
        {
            base.OnDataLoading();
            ViewModel.ViewerData = ProductCategoryService.GetAllProductCategories().ToObservableCollection();
        }

        public ReadOnlyCollection<FrameworkElement> ToolBarItems { get; private set; }
    }
}
